/**
 * @file GlobalState.java
 *
 * Maintain a global state of a variable.
 *
 * Author: DKE Aerospace Germany GmbH
 *
 * Copyright 2011 European Commission
 *
 * Licensed under the EUPL, Version 1.1 only (the "Licence");
 * You may not use this work except in compliance with the
 * Licence.
 * You may obtain a copy of the Licence at:
 * http://ec.europa.eu/idabc/eupl
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 *
 **/
package com.dkeaerospace.egnosdemoapp;

import java.io.InputStream;
import java.io.OutputStream;

import android.app.Application;
import android.bluetooth.BluetoothSocket;

/**
 * Class to maintain a global state of a variable.
 **/
public class GlobalState {
    private BluetoothSocket BTsocket = null;
    private InputStream inputStream = null;
    private OutputStream outputStream = null;
    private static double[] position = new double[10];
    private static double[] initPosition = new double[4];
    private static int egnos = 0;
    private static int sisnet = 0;
    private static int receiverType = 0;
    private static int network = 0;
    private static int numSVInt = 0;
    private static int SVId = 0;
    private static int Gps = 0;
    private String username = "";
    private String password = "";
    String address = "";
    static int mtcSfrb = 0;
    static int mtcRaw = 0;
    private static boolean isCurrent = false;
    private static boolean isTracking = false;
    private static int isLoggedFile = 0;

    private static GlobalState globalState;

    private GlobalState() {
    }

    public synchronized static GlobalState getInstance() {
        if (globalState == null) {
            globalState = new GlobalState();
        }
        return globalState;
    }

    /**
     * setisCurrent function. 
     * Sets TRUE or FALSE indicating Current Location is
     * currently running or not.
     * 
     * @param newisCurrent  TRUE or FALSE based on Current Location functionality.
     **/
    public final static void setisCurrent( boolean newisCurrent ) {
        isCurrent = newisCurrent;
    }

    /**
     * getNetwork function. 
     * Gets TRUE or FALSE indicating Current Location is
     * currently running or not.
     * 
     * @return isCurrent TRUE or FALSE based on Current Location functionality.
     **/
    public final static boolean getisCurrent() {
        return isCurrent;
    }

    /**
     * setisTracking function. 
     * Sets TRUE or FALSE indicating Start Tracking is
     * currently running or not.
     * 
     * @param newNetwork   TRUE or FALSE based on Start Tracking functionality.
     **/
    public final static void setisTracking( boolean newisTracking ) {
        isTracking = newisTracking;
    }

    /**
     * getisTracking function. 
     * Gets TRUE or FALSE, indicating Start Tracking is
     * currently running or not. network = 1 if network for the device is not
     * available, otherwise network = 0.
     * 
     * @return isTracking TRUE or FALSE based on Start Tracking functionality.
     **/
    public final static boolean getisTracking() {
        return isTracking;
    }

    /**
     * setisLogFile function.
     * Sets TRUE or FALSE if log file was created.
     * 
     * @param setLog  TRUE or FALSE if log file was created.
     **/
    public final static void setisLogFile( int setLog ) {
        isLoggedFile = setLog;
    }

    /**
     * getisLogFile function. 
     * Gets Sets TRUE or FALSE if log file was created.
     * 
     * @return isLogFile TRUE or FALSE if log file was created.
     **/
    public final static int getisLogFile() {
        return isLoggedFile;
    }

    /**
     * setNetwork function. 
     * Sets network to 1 if network is available, otherwise 0.
     * 
     * @param newNetwork    0 or 1 based on availability of network.
     **/
    public final static void setNetwork( int newNetwork ) {
        network = newNetwork;
    }

    /**
     * getNetwork function. 
     * Gets the value 1 or 0, indicating network is available
     * or not. network = 1 if network for the device is not available, otherwise
     * network = 0.
     * 
     * @return network     0 or 1 based on availability of network.
     **/
    public final static int getNetwork() {
        return network;
    }

    /**
     * setReceiverType function. 
     * Sets the type of the external Bluetooth Receiver.
     * 
     * @param receiverType   type of the external Bluetooth Receiver.
     **/
    public final static void setReceiverType( int newReceiverType ) {
        receiverType = newReceiverType;
    }

    /**
     * getReceiverType function. 
     * Gets the type of the external Bluetooth Receiver.
     * 
     * @return receiverType type of the external Bluetooth Receiver.
     **/
    public final static int getReceiverType() {
        return receiverType;
    }

    /**
     * setEgnos function. 
     * Sets the value 1 or 0 indicating EGNOS is ON or OFF from Settings.
     * 
     * @param newEgnos   0 or 1 based on EGNOS ON or OFF from Settings.
     **/
    public final static void setEgnos( int newEgnos ) {
        egnos = newEgnos;
    }

    /**
     * getEgnos function. 
     * Gets value 1 or 0 indicating EGNOS is ON or OFF from Settings.
     * 
     * @return egnos 0 or 1 based on EGNOS ON or OFF from Settings.
     **/
    public final static int getEgnos() {
        return egnos;
    }

    /**
     * setSISNeT function. 
     * Sets the value 1 or 0 indicating SISNeT is ON or OFF from Settings.
     * 
     * @param newSisnet    0 or 1 based on SISNeT ON or OFF from Settings.
     **/
    public final static void setSISNeT( int newSisnet ) {
        sisnet = newSisnet;
    }

    /**
     * getSISNeT function.  
     * Gets the value 1 or 0 indicating SISNeT is ON or OFF from Settings.
     * 
     * @return sisnet      0 or 1 based on SISNeT ON or OFF from Settings.
     **/
    public final static int getSISNeT() {
        return sisnet;
    }

    /**
     * setnumSVInt function.
     * Sets the number of Satellite Vehicles in view.
     * 
     * @param newnumSVInt   number of Satellite Vehicles in view.
     **/
    public final static void setnumSVInt( int newnumSVInt ) {
        numSVInt = newnumSVInt;
    }

    /**
     * getnumSVInt function. 
     * Gets the number of Satellite Vehicles in view.
     * 
     * @return numSVInt    number of Satellite Vehicles in view.
     **/
    public final static int getnumSVInt() {
        return numSVInt;
    }

    /**
     * setSVId function. 
     * Sets the ID of the Satellite transmitting subframe.
     * 
     * @param newSVId      new Satellite vehicle ID.
     **/
    public final static void setSVId( int newSVId ) {
        SVId = newSVId;
    }

    /**
     * getSVId function 
     * Gets the Satellite Vehicle ID.
     * 
     * @return SVId       Satellite vehicle ID.
     **/
    public final static int getSVId() {
        return SVId;
    }

    /**
     * setGps function 
     * Sets the availability of GPS data. Sets Gps to 1 if number
     * of satellites in view is more than 4, otherwise 0.
     * 
     * @param newGps      sets 0 or 1 based on GPS data availability.
     **/
    public final static void setGps( int newGps ) {
        Gps = newGps;
    }

    /**
     * getGps function 
     * Gets the value of gps, indicating GPS data is available or
     * not. Gps = 1 if number of satellites in view is more than 4,
     * otherwise Gps = 0.
     * 
     * @return Gps        0 or 1 based on availability of GPS data.
     **/
    public final static int getGps() {
        return Gps;
    }

    /**
     * setPosition function 
     * Sets the current position acquired. 
     * Rows 0 to 6 as GPS Latitude, GPS Longitude, GPS Altitude, 
     * EGNOS Latitude, EGNOS Longitude,EGNOS Altitude and HPL respectively.
     * 
     * @param newPosition   array of current position.
     **/
    public final static void setPosition( final double[] newPosition ) {
        position = newPosition;
    }

    /**
     * getPosition function 
     * Gets the computed position as a table of 7 x 1 values.
     * Rows 0 to 6 as GPS Latitude, GPS Longitude, GPS Altitude, EGNOS Latitude,
     * EGNOS Longitude, EGNOS Altitude and HPL respectively.
     * 
     * @return position     array of current position.
     **/
    public final static double[] getPosition() {
        return position;
    }

    /**
     * setinitialPosition function 
     * Sets the initial position acquired. 
     * The position in the ECEF (xyz and clock bias * speed of light).
     * Used as a first estimation to obtain a position.
     * 
     * @param newInitialPosition   array of initial position.
     **/
    public final static void setinitialPosition( final double[] newInitialPosition ) {
        initPosition = newInitialPosition;
    }

    /**
     * getinitialPosition function 
     * Gets the initial position acquired.
     * The position in the ECEF (xyz and clock bias * speed of light).
     * Used as a first estimation to obtain a position.
     * 
     * @return initPosition      array of initial position.
     **/
    public final static double[] getinitialPosition() {
        return initPosition;
    }
    /**
     * getAddress function 
     * Gets address of external Bluetooth receiver.
     * 
     * @return address     Address of external Bluetooth receiver.
     **/
    public final String getAddress() {
        return address;
    }

    /**
     * setAddress function Sets address of external Bluetooth receiver.
     * 
     * @param newAddress   Address of external Bluetooth receiver.
     **/
    public final void setAddress( final String newAddress ) {
        address = newAddress;
    }

    /**
     * getMct function 
     * Gets the mct for Sfrb and Raw message.
     * 
     * @return sr     mct for Sfrb and Raw message.
     **/
    public final static String getMct() {
        String sr = "SFRB: " + mtcSfrb + ", RAW: " + mtcRaw;
        return sr;
    }

    /**
     * setMctSfrb function 
     * Sets the mct Sfrb message.
     * 
     * @return mtcSfrb     mct Sfrb message.
     **/
    public static final void setMctSfrb() {
        mtcSfrb++;
    }

    /**
     * setMctRaw function 
     * Sets the mct Raw message.
     * 
     * @return mtcRaw     mct Raw message.
     **/
    public final static void setMctRaw() {
        mtcRaw++;
    }

    /**
     * getSocket function 
     * Gets an Rfcomm bluetooth socket for external Bluetooth
     * receiver connection.
     * 
     * @return BTsocket   Rfcomm bluetooth socket for external Bluetooth 
     *                    receiver connection.
     **/
    public final BluetoothSocket getSocket() {
        return BTsocket;
    }

    /**
     * setSocket function 
     * Sets an Rfcomm bluetooth socket for external Bluetooth
     * receiver connection.
     * 
     * @param newBtSocket  Rfcomm bluetooth socket for external Bluetooth
     *                     receiver connection.
     **/
    public final void setSocket( final BluetoothSocket newBtSocket ) {
        BTsocket = newBtSocket;
    }

    /**
     * getInputStream function 
     * Gets an input stream for external Bluetooth
     * receiver connection.
     * 
     * @return inputStream   input stream for external Bluetooth receiver
     *                       connection.
     **/
    public final InputStream getInputStream() {
        return inputStream;
    }

    /**
     * setInputStream function 
     * Sets an input stream for external Bluetooth
     * receiver connection.
     * 
     * @param newInputStream   input stream for external Bluetooth receiver 
     *                         connection.
     **/
    public final void setInputStream( final InputStream newInputStream ) {
        inputStream = newInputStream;
    }

    /**
     * getOutputStream function 
     * Gets an output stream for external Bluetooth
     * receiver connection.
     * 
     * @return outputStream    output stream for external Bluetooth receiver
     *                         connection.
     **/
    public final OutputStream getOutputStream() {
        return outputStream;
    }

    /**
     * setOutputStream function 
     * Sets an output stream for external Bluetooth
     * receiver connection.
     * 
     * @param newOutputStream   output stream of external Bluetooth receiver 
     *                          connection.
     **/
    public final void setOutputStream( final OutputStream newOutputStream ) {
        outputStream = newOutputStream;
    }

    /**
     * getSISNeTUsername function 
     * Gets username of SISNeT Login for Settings.
     * 
     * @return username        username of SISNeT Login for Settings.
     **/
    public final String getSISNeTUsername() {
        return username;
    }

    /**
     * setSISNeTUsername function 
     * Sets username of SISNeT Login for Settings.
     * 
     * @param newUserName       username of SISNeT Login for Settings.
     **/
    public final void setSISNeTUsername( final String newUserName ) {
        username = newUserName;
    }

    /**
     * getSISNeTPassword function
     * Gets password of SISNeT Login for Settings.
     * 
     * @return password          password of SISNeT Login for Settings.
     **/
    public final String getSISNeTPassword() {
        return password;
    }

    /**
     * setSISNeTPassword function 
     * Sets password of SISNeT Login for Settings.
     * 
     * @param newPassword       password of SISNeT Login for Settings.
     **/
    public final void setSISNeTPassword( final String newPassword ) {
        password = newPassword;
    }
}
